<?php
/**
 *@todo Fang
 +------------------------------------------------------------------------------
 * @version     1.0
 * @since       2015-03-22
 +------------------------------------------------------------------------------
 */
namespace Admin\Controller;
class FangController extends AdminController {
    
    /**
     * @todo    使用工具页
     */
    public function index(){
        $this->getActivityList();
        $this->assign('meta_title', '常用工具');
        $this->display('index');
    }
    
    private function getActivityList($map=array('status'=>1),$order='sort desc',$field="activity_id,activity_name"){
        $activity_list = D('activity')->getActivityList($map,$order,$field);
        $this->assign('activity_list',$activity_list);
    }
    
    /**
     * @todo    网推渠道报名数据/签单
     */
    public function exportQ(){
        
        $activity_shop = array(
            '19' => 258,
            '28' => 259,
        );
        //198-19-258    499-28-259
        $activity_id = (int)I('activity_id');
        $is_order = (int)I('is_order');
        $shop_id = (int)$activity_shop[$activity_id];
        $name = '';
        
        if($activity_id == 0){
            $this->error('参数错误');
        }
        // 生成excel
        $cell = array(
            'A' => array('渠道'),
            'B' => array('报名时间', 20),
            'C' => array('票别'),
        );
        
        $join = 'left join iweb_member on iweb_member.uid = iweb_activity_join.generalize_member_id';
        $field = 'login_name,FROM_UNIXTIME(join_time),ticket_level';
        if($is_order > 0 && $shop_id > 0){
            $field .= ',customer_id';
        }
        $where = array(
            'activity_id' => $activity_id,
            'generalize_member_id' => array('BETWEEN', '502,523'),
        );
        $order = 'join_time desc';
        $data = M('activity_join')->join($join)->field($field)->where($where)->order($order)->select();
        
        // 是否签单
        if($is_order > 0 && $shop_id > 0){
            
            $name = '-含是否签单';
            // 添加栏目
            $cell['D'] = array('是否签单');
            // 获取签单用户
            $customer_ids = array_column($data, 'customer_id');
            $order_where['shop_id'] = $shop_id;
            $order_where['activity_id'] = 25;   // 16届家博会
            $order_where['order_status'] = array('in', '2,4,5,7');
            $order_where['customer_id'] = array('in', $customer_ids);
            $order_customer = M('order')->where($order_where)->getField('customer_id', true);
            foreach ($data as $key=>$val){
                if(in_array($val['customer_id'], $order_customer)){
                    $data[$key]['is_order'] = 1;
                }else{
                    $data[$key]['is_order'] = 0;
                }
                unset($data[$key]['customer_id']);
            }
        }
        
        $is_excel = dataToExcel($cell, $data, '渠道报名', getActivityName($activity_id).'-渠道报名'.$name);

        if($is_excel !== false){
            $result['status'] = 1;
            $result['info'] = $is_excel;
        }
        $this->ajaxReturn($result);
    }
    
    /**
     * @todo    网推自然报名数据
     */
    public function exportZ(){

        $activity_id = (int)I('activity_id');
        if($activity_id == 0){
            $this->error('参数错误');
        }
        // 生成excel
        $cell = array(
            'A' => array('报名时间', 20),
            'B' => array('票别'),
        );
    
        $field = 'FROM_UNIXTIME(join_time),ticket_level';
        $where = array(
            'activity_id' => $activity_id,
            'sign_channel' => 0,
            'generalize_member_id' => 0,
        );
        $order = 'join_time desc';
        $data = M('activity_join')->field($field)->where($where)->order($order)->select();
    
        $is_excel = dataToExcel($cell, $data, '自然报名', getActivityName($activity_id).'-自然报名');
    
        if($is_excel !== false){
            $result['status'] = 1;
            $result['info'] = $is_excel;
        }
        $this->ajaxReturn($result);
    }
    
    /**
     * @todo    获取16届未报名15届vip用户数据
     */
    public function getVip(){
        $vip_15 = M('activity_join')->where(array('activity_id' => 21, 'ticket_level' => 1))->getField('customer_id', true);
        $customer_16 = M('activity_join')->where(array('activity_id' => 25, 'customer_id' => array('in', $vip_15)))->getField('customer_id', true);
        $arr = array_diff($vip_15, $customer_16);
        $customer = M('customer')->where(array('customer_id' => array('in', $arr)))->getField('mobile_phone', true);
        foreach ($customer as $val){
            //echo $val.'<br>';
        }
    }
    
    /**
     * @todo    普通票－成单－推广人（用户运营中心）
     */
    public function export_1(){

        // 获取参数
        $activity_id = (int)I('activity_id');
        $tecket_levle = I('tecket_levle');
        if($activity_id == 0){
            $this->error('参数错误');
        }
        
        // 获取成单用户
        $where = array(
            'activity_id' => $activity_id,
            'order_status' => array('in', '2,4,5,7'),
        );
        $join = 'left join iweb_customer on iweb_customer.customer_id = iweb_order.customer_id';
        $customer_list = M('order')->join($join)->field('iweb_order.customer_id,mobile_phone')->where($where)->select();
        // 数据处理
        $customer_ids = array_column($customer_list, 'customer_id');
        $customer_phones = array_column($customer_list, 'mobile_phone', 'customer_id');
        
        // 获取推广成单用户 4.2日前
        $g_where = array(
            'activity_id' => $activity_id,
            'generalize_member_id' => array('gt', 0),
            'join_time' => array('lt', 1459526400),
            'customer_id' => array('in', $customer_ids),
        );
        // 票别
        if($tecket_levle >= 0){
            $g_where['ticket_level'] = $tecket_levle;
        }
        
        $g_join = 'left join iweb_member on iweb_member.uid = iweb_service_activity_join.generalize_member_id';
        $g_customer_ids = M('service_activity_join')->join($g_join)->where($g_where)->getField('customer_id,generalize_member_id,member_name', true);
        
        // 数据处理
        $data = array();
        foreach ($customer_ids as $key=>$val){
            if(isset($g_customer_ids[$val]['generalize_member_id'])){
                $data[] = array(
                    'customer' => $customer_phones[$val],
                    'generalize_member' => $g_customer_ids[$val]['member_name'],
                );
            }
        }
        // 数组排序
        array_multisort($data);
        
        // 定义导出字段
        $cell = array(
            'A' => array('成单客户', 15),
            'B' => array('推广人', 15),
        );
        
        // 导出数据生成excel
        $is_excel = dataToExcel($cell, $data, '普通票成单的推广人', getActivityName($activity_id).'-普通票成单的推广人数据');

        if($is_excel !== false){
            $result['status'] = 1;
            $result['info'] = $is_excel;
        }
        $this->ajaxReturn($result);
    }
    
    /**
     * @todo    推广人成单数统计（用户运营中心）
     */
    public function export_2(){
        
        // 获取参数
        $activity_id = (int)I('activity_id');
        $tecket_levle = I('tecket_levle');
        $type = (int)I('type');
        
        if($activity_id == 0){
            $this->error('参数错误');
        }
        
        // 获取成单用户
        $where = array(
            'activity_id' => $activity_id,
            'order_status' => array('in', '2,4,5,7'),
        );
        $customer_ids = M('order')->where($where)->getField('customer_id', true);
        
        // 户数和单数区别
        if($type > 0){
            $customer_ids = array_unique($customer_ids);
        }
        
        // 获取推广成单用户 4.2日前
        $g_where = array(
            'activity_id' => $activity_id,
            'generalize_member_id' => array('gt', 0),
            'join_time' => array('lt', 1459526400),
            'customer_id' => array('in', $customer_ids),
        );
        // 票别
        if($tecket_levle >= 0){
            $g_where['ticket_level'] = $tecket_levle;
        }
        
        $g_join = 'left join iweb_member on iweb_member.uid = iweb_service_activity_join.generalize_member_id';
        $g_customer_ids = M('service_activity_join')->join($g_join)->where($g_where)->getField('customer_id,generalize_member_id,member_name', true);
        
        $data = array();
        foreach ($customer_ids as $key=>$val){
            if(!isset($g_customer_ids[$val]['generalize_member_id'])){
                continue;
            }
            if(isset($data[$g_customer_ids[$val]['generalize_member_id']])){
                $data[$g_customer_ids[$val]['generalize_member_id']]['order_count']++;
            }else{
                $data[$g_customer_ids[$val]['generalize_member_id']] = array(
                    'member_name' => $g_customer_ids[$val]['member_name'],
                    'order_count' => 1,
                );
            }
        }
        
        // 定义导出字段
        $cell = array(
            'A' => array('推广人', 15),
            'B' => array('成单数', 15),
        );
        
        // 导出数据生成excel
        $is_excel = dataToExcel($cell, $data, '推广人成单', getActivityName($activity_id).'-推广人成单数据');
        
        if($is_excel !== false){
            $result['status'] = 1;
            $result['info'] = $is_excel;
        }
        $this->ajaxReturn($result);
        
    }
    
    /**
     * @todo    vip绑定人对应的成单数（用户运营中心）
     */
    public function export_3(){
        
        // 获取参数
        $activity_id = (int)I('activity_id');
        
        $activity_id = 25;
        
        if($activity_id == 0){
            $this->error('参数错误');
        }
        
//         // 获取绑定人 4.2日前
//         $g_where = array(
//             'activity_id' => $activity_id,
//             'bind_member_id' => array('gt', 0),
//             'join_time' => array('lt', 1459526400),
//             'ticket_level' => 1,
//         );
        
//         $g_join = 'left join iweb_member on iweb_member.uid = iweb_service_activity_join.bind_member_id';
//         $g_customer_ids = M('service_activity_join')->join($g_join)->where($g_where)->getField('customer_id,bind_member_id,member_name', true);
        
//         $g_customer = array_column($g_customer_ids, 'customer_id');
        
        // 获取订单数
        $where = array(
            'activity_id' => $activity_id,
            'order_status' => array('in', '2,4,5,7'),
        );
        $order = M('order')->where($where)->field('count(order_id) as count,customer_id')->group('customer_id')->select();
        $order_arr = array_column($order, 'count', 'customer_id');
        
        // 绑定人
        $member_ids = M('service_my_customer')->getField('member_id', true);
        $member_ids = array_unique($member_ids);
        
        // 获取绑定人 4.2日前
        $g_where = array(
            'activity_id' => $activity_id,
            'member_id' => array('gt', 0),
            'join_time' => array('lt', 1459526400),
            'ticket_level' => 1,
        );
        $g_join = 'left join iweb_service_my_customer on iweb_service_my_customer.customer_id = iweb_service_activity_join.customer_id join iweb_member on iweb_member.uid = iweb_service_my_customer.member_id ';
        $g_customer_ids = M('service_activity_join')->join($g_join)->where($g_where)->getField('iweb_service_activity_join.customer_id,member_id,member_name', true);
        
        $data = array();
        foreach ($g_customer_ids as $key=>$val){
            if(isset($data[$val['member_id']])){
                $data[$val['member_id']]['order_count'] += $order_arr[$val['customer_id']]; 
            }else{
                if(isset($order_arr[$val['customer_id']])){
                    $data[$val['member_id']] = array(
                        'member_name' => $val['member_name'],
                        'order_count' => (int)$order_arr[$val['customer_id']],
                    );
                }
            }
        }
        
        // 定义导出字段
        $cell = array(
            'A' => array('绑定人', 15),
            'B' => array('成单数', 15),
        );
        
        // 导出数据生成excel
        $is_excel = dataToExcel($cell, $data, '绑定人成单', getActivityName($activity_id).'-绑定人成单数据');
        
        if($is_excel !== false){
            $result['status'] = 1;
            $result['info'] = $is_excel;
        }
        $this->ajaxReturn($result);
    }
    
    /**
     * @todo    非推广成单用户（用户运营中心）
     */
    public function export_4(){
        
        // 获取参数
        $activity_id = (int)I('activity_id');
        
        if($activity_id == 0){
            $this->error('参数错误');
        }        
        // 抛弃店铺获取
        $s_where = array(
            'conf_category_id' => array('in', '19,20,21,23,28'),
            'status' => 1,
        );
        $shop_ids = M('shop')->where($s_where)->getField('shop_id', true);
        
        // 获取订单数
        $where = array(
            'activity_id' => $activity_id,
            'order_status' => array('in', '2,4,5,7'),
            'shop_id' => array('not in', $shop_ids),
        );
        $customer_ids = M('order')->where($where)->getField('customer_id', true);
        $customer_ids = array_unique($customer_ids);

        // 获取非推广成单用户
        $g_where = array(
            'activity_id' => $activity_id,
            'generalize_member_id' => 0,
            'customer_id' => array('in', $customer_ids),
        );
        $customer = M('service_activity_join')->where($g_where)->getField('customer_id', true);

        // 获取手机号
        $data = M('customer')->field('mobile_phone')->where(array('customer_id' => array('in', $customer)))->limit(2000)->select();

        // 定义导出字段
        $cell = array(
            'A' => array('手机号', 20),
        );
        
        // 导出数据生成excel
        $is_excel = dataToExcel($cell, $data, '非推广成单用户', getActivityName($activity_id).'-非推广成单用户数据');
        
        if($is_excel !== false){
            $result['status'] = 1;
            $result['info'] = $is_excel;
        }
        $this->ajaxReturn($result);
    }
    
    /**
     * @todo    绑定杜金
     */
    public function nengman_dj(){
        
        exit();
        
        $activity_id = 25;  // 16th家博会
        $dj = 31;
        
        // 获取普通票成单用户
        $where = array(
            'activity_id' => $activity_id,    
            'generalize_member_id' => 0,
        );
        $customer = M('service_activity_join')->field('customer_id')->where($where)->select();
        // 16th 成单用户
        $customer_arr = array_column($customer, 'customer_id');
        $buy_where = array(
            'activity_id' => $activity_id,
            'order_status' => array('in', '2,4,5,7'),
        );
        $buy_customer = M('order')->field('customer_id')->where($buy_where)->select();
        $buy_customer_arr = array_merge(array_flip(array_flip(array_column($buy_customer, 'customer_id'))));
        // 16th 家博会普通票成单数据（绑给杜金）31
        $buy_customer_ids = array_intersect($customer_arr, $buy_customer_arr);
        
        $buy_save_where = array(
            'activity_id' => $activity_id,
            'customer_id' => array('in', $buy_customer_ids),
        );
        $dj_data = array();
        foreach ($buy_customer_ids as $key=>$val){
            $dj_data[] = array(
                'customer_id' => (int)$val,
                'member_id' => $dj,
            );
        }
        
        // 事务1－杜金绑定
        //         M('service_my_customer')->startTrans();
        // 删除绑定信息
        M('service_my_customer')->where(array('customer_id' => array('in', $buy_customer_ids)))->delete();
        //         if(false === M('service_my_customer')->where(array('customer_id' => array('in', $buy_customer_ids)))->delete()){
        //             M('service_my_customer')->rollback();
        //             return 'service_my_customer delete error';
        //         }
        // 添加绑定信息
        M('service_my_customer')->addAll($dj_data);
        //         if(false === M('service_my_customer')->addAll($dj_data)){
        //             M('service_my_customer')->rollback();
        //             return 'service_my_customer add error';
        //         }
        // 修改service_activity_join中 用户的绑定状态31
        M('service_activity_join')->where($buy_save_where)->setField('bind_member_id', $dj);
        //         if(false === M('service_activity_join')->where($buy_save_where)->setField('bind_member_id', 31)){
        //             M('service_my_customer')->rollback();
        //             return 'service_activity_join update error';
        //         }
        //         M('service_my_customer')->commit();
        echo 'success';
    }
    
}